package com.example.restatr.Atcivity.Main.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.restatr.Atcivity.Main.OrderListAt;
import com.example.restatr.Atcivity.Main.UserCenterAt;
import com.example.restatr.Atcivity.Main.LoginAt;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Atcivity.Main.SetPswAt;
import com.example.restatr.Tools;
import com.example.restatr.Atcivity.Main.FeedBackAt;
import com.google.android.material.navigation.NavigationView;

import org.json.JSONException;
import org.json.JSONObject;

public class UserFragment extends Fragment {
    private NavigationView Nv;
    private ImageView head;
    private TextView nickName;
    private TextView userId;
    private ImageView setInfo;
    public static String token;
    public static int code;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.fragment_gr, container, false );
        Nv = view.findViewById( R.id.Nv );
        head = Nv.getHeaderView( 0 ).findViewById( R.id.head );
        setInfo = Nv.getHeaderView( 0 ).findViewById( R.id.setInfo );
        nickName = Nv.getHeaderView( 0 ).findViewById( R.id.nickName );
        userId = Nv.getHeaderView( 0 ).findViewById( R.id.userId );
        token = MainActivity.sp.getString( "token", null );
        code = MainActivity.sp.getInt( "code", 0 );
        Nv.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.grxx:
                        startAtForResult( UserCenterAt.class, "您还未登入！" );
                        break;
                    case R.id.ddlb:
                        startAt( OrderListAt.class);
                        break;
                    case R.id.xgmm:
                        startAt( SetPswAt.class);
                        break;
                    case R.id.yjfk:
                        startAt( FeedBackAt.class);
                        break;
                    case R.id.exit:
                        if (code == 200) {
                            code = 0;
                            MainActivity.edit.putInt( "code", 0 ).apply();
                            Toast.makeText( getActivity(), "退出成功！", Toast.LENGTH_SHORT ).show();
                            SetInfo();
                        } else {
                            Toast.makeText( getActivity(), "您还未登入！", Toast.LENGTH_SHORT ).show();
                        }
                        break;

                }
                return true;
            }
        } );
        nickName.setOnClickListener( new LoginClick() );
        head.setOnClickListener( new LoginClick() );
        setInfo.setOnClickListener( new LoginClick() );
        SetInfo();
        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult( requestCode, resultCode, data );
        SetInfo();
    }

    private void SetInfo() {
        if (code == 200) {
            new Thread( new Runnable() {
                @Override
                public void run() {
                    final String get = Tools.Get( "/prod-api/api/common/user/getInfo", token );
                    if (get != null && getActivity() != null) {
                        getActivity().runOnUiThread( new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject jsonObject = new JSONObject( get );
                                    int code = jsonObject.getInt( "code" );
                                    if (code == 200) {
                                        JSONObject user = jsonObject.getJSONObject( "user" );
                                        userId.setText( "ID:" + user.getString( "userId" ) );
                                        MainActivity.edit.putInt("userId",user.getInt("userId")).apply();
                                        nickName.setText( user.getString( "nickName" ) );
                                        if (getActivity() != null) {
                                            Glide.with( getActivity() )
                                                    .load( Tools.ip+user.getString( "avatar" ) )
                                                    .transform( new CircleCrop() )
                                                    .placeholder( R.drawable.ic_baseline_account_circle_24 )
                                                    .into( head );
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        } );
                    }
                }
            } ).start();
        } else {
            userId.setText( "ID:" );
            nickName.setText( "点击登入" );
            head.setImageResource( R.drawable.ic_baseline_account_circle_24 );
        }
    }

    private void startAt(Class cls) {
        if (code == 200) {
            startActivity( new Intent( getActivity(), cls ) );
        } else {
            Toast.makeText( getActivity(), "您还未登入!", Toast.LENGTH_SHORT ).show();
        }
    }

    private void startAtForResult(Class cls, String msg) {
        if (code == 200) {
            startActivityForResult( new Intent( getActivity(), cls ), 0 );
        } else {
            Toast.makeText( getActivity(), msg, Toast.LENGTH_SHORT ).show();
        }
    }

    private void startAtForResult() {
        if (code == 200) {
            startActivityForResult( new Intent( getActivity(), UserCenterAt.class ), 0 );
        } else {
            startActivityForResult( new Intent( getActivity(), LoginAt.class ), 0 );
        }
    }

    class LoginClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.setInfo:
                    startAtForResult( UserCenterAt.class, "您还未登入！" );
                    break;
                case R.id.nickName:
                case R.id.head:
                    startAtForResult();
                    break;
            }
        }

    }
}